﻿/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */

using dnAnalytics.LinearAlgebra;
using Microsoft.VisualStudio.DebuggerVisualizers;

namespace dnAnalytics.DebugVisualizers
{
    /// <summary>
    /// A visualizer which displays the values of a <see cref="Matrix"/> object.
    /// </summary>
    public class MatrixValueVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            Matrix matrix = (Matrix)objectProvider.GetObject();

            // See if the matrix is a sparse one. If so then display
            // it that way because we'll be able to display it on
            // a special form.
            {
                SparseMatrix sparseMatrix = matrix as SparseMatrix;
                if (sparseMatrix != null)
                {
                    DisplaySparseMatrix(windowService, sparseMatrix);
                    return;
                }
            }

            // The matrix is not sparse so it could be dense
            // Check that and display it as a dense one if we
            // can.
            {
                DenseMatrix denseMatrix = matrix as DenseMatrix;
                if (denseMatrix != null)
                {
                    DisplayDenseMatrix(windowService, denseMatrix);
                    return;
                }
            }

            // We don't know which type the matrix is so we'll just
            // turn it into a dense one and display that.
            {
                DisplayGeneralMatrix(windowService, matrix);
            }
        }

        private void DisplaySparseMatrix(IDialogVisualizerService windowService, SparseMatrix matrix)
        {
            MatrixProperties matrixProperties = new SparseMatrixProperties(matrix);
            DisplayMatrix(windowService, matrixProperties);
        }

        private void DisplayDenseMatrix(IDialogVisualizerService windowService, DenseMatrix matrix)
        {
            MatrixProperties matrixProperties = new DenseMatrixProperties(matrix);
            DisplayMatrix(windowService, matrixProperties);
        }

        private void DisplayGeneralMatrix(IDialogVisualizerService windowService, Matrix matrix)
        {
            DenseMatrix denseMatrix = new DenseMatrix(matrix);
            DisplayDenseMatrix(windowService, denseMatrix);
        }

        private void DisplayMatrix(IDialogVisualizerService windowService, MatrixProperties matrix)
        {
            using (var displayForm = new MatrixVisualizerForm())
            {
                displayForm.Matrix = matrix;
                windowService.ShowDialog(displayForm);
            }
        }

        /// <summary>
        /// Tests the visualizer by hosting it outside of the debugger.
        /// </summary>
        /// <param name="objectToVisualize">The object to display in the visualizer.</param>
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(MatrixValueVisualizer));
            visualizerHost.ShowVisualizer();
        }
    }
}
